package com.itheima.publisher.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class MqConfig {

    private final RabbitTemplate rabbitTemplate;


    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(returned -> {
            log.error("监听到了消息return callback");
            log.debug("exchange: {}", returned.getExchange());
            log.debug("routingKey: {}", returned.getRoutingKey());
            log.debug("message: {}", returned.getMessage());
            log.debug("replyCode: {}", returned.getReplyCode());
            log.debug("replyText: {}", returned.getReplyText());
        });

        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                log.info("消息发送到交换机成功！correlationData：" + correlationData);
                log.info("消息发送到交换机成功！消息id：" + correlationData.getId());
                log.info("消息发送到交换机成功！getFuture：" + correlationData.getFuture());
                log.info("消息发送到交换机成功！getReturned：" + correlationData.getReturned());
            } else {
                log.info("消息发送到交换机失败！数据：" + correlationData + " 原因：" + cause);
            }
        });
    }






  /*  @PostConstruct
    public void init1(){
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                log.error("监听到了消息return callback");
                log.debug("exchange: {}", returnedMessage.getExchange());
                log.debug("routingKey: {}", returnedMessage.getRoutingKey());
                log.debug("message: {}", returnedMessage.getMessage());
                log.debug("replyCode: {}", returnedMessage.getReplyCode());
                log.debug("replyText: {}", returnedMessage.getReplyText());
            }

         rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack) {
                    log.info("消息发送到交换机成功！correlationData：" + correlationData);
                    log.info("消息发送到交换机成功！消息id：" + correlationData.getId());
                    log.info("消息发送到交换机成功！getFuture：" + correlationData.getFuture());
                    log.info("消息发送到交换机成功！getReturned：" + correlationData.getReturned());
                } else {
                    log.info("消息发送到交换机失败！数据：" + correlationData + " 原因：" + cause);
                }
            }
        });
        });
    }*/

}
